// interceptBigMap-----------------------------------------------------------------------------
TrvnInterceptorService.installInterceptor( {
	name : "big-map.BigMap",
	isAvailble : function() {
		return true;
	},
	intercept : function(doc) {
		if (!doc.URL.contains("karte.php")) return;
		var tb = doc.getElementById("tb");
		if (!tb) return;
		var buttonDiv = doc.$C("div");
		buttonDiv.style.position = "absolute";
		buttonDiv.style.width = "33px";
		buttonDiv.style.height = "25px";
		buttonDiv.style.zIndex = "65";
		buttonDiv.style.left = "100px";
		buttonDiv.style.top = "88px";
		var buttonImg = doc.$C("img");
		buttonImg.title = "大地图";
		buttonImg.alt = "大地图";
		buttonImg.border = 0;
		buttonImg.width = 33;
		buttonImg.height = 25;
		buttonImg.src = TrvnCfg.queryUrl("img/un/m/max.gif");
		buttonImg.style.cursor = "pointer";
		buttonDiv.appendChild(buttonImg);
		tb.parentNode.insertBefore(buttonDiv, tb);
		buttonImg.addEventListener('click', function() {
			var mapWindow = doc.ownerWindow.open("", "map", "top=100,left=25,width=975,height=550");
			mapWindow.focus();
			var xp = TrvnUtils.parseNumber(doc.getElementsByName("xp")[0].value);
			var yp = TrvnUtils.parseNumber(doc.getElementsByName("yp")[0].value);
			var mapDoc = mapWindow.document;
			TrvnUtils.initDoc(mapDoc);
			var css = mapDoc.$C("link");
			css.rel = "stylesheet";
			css.type = "text/css";
			css.href = TrvnCfg.queryUrl("unx.css");
			mapDoc.getElementsByTagName("head")[0].appendChild(css);
			setMapXY(mapDoc, xp, yp);
		}, false);
	}
});
function DrawMapParseCallback(pos, mapDoc, xp, yp) {
	var mapObj = mapDoc.getElementById("trvnmap");
	var mapDiv = mapDoc.getElementById("trvnMapDiv");

	var addEvent = function(obj, event, jsCode) {
		obj.addEventListener(event, function() {
			eval(jsCode);
		}, false);
	}

	var map = function(dorf, spieler, ew, ally, x, y) {
		var text_details = '详情:';
		var text_spieler = '玩家:';
		var text_einwohner = '居民:';
		var text_allianz = '联盟:';
		mapDoc.getElementById('x').firstChild.nodeValue = x;
		mapDoc.getElementById('y').firstChild.nodeValue = y;
		var myElement = mapDoc.getElementById("tb");
		if (myElement != null) {
			if (ally == '') {
				ally = '-';
			}
			var tbt = "<table cellspacing='1' cellpadding='2' class='tbg f8'><tr><td class='rbg f8' colspan='2'></a>"
					+ dorf + "</td></tr><tr><td width='45%' class='s7 f8'>" + text_spieler + "</td><td class='s7 f8'>"
					+ spieler + "</td></tr><tr><td class='s7 f8'>" + text_einwohner + "</td><td class='s7 f8' id='ew'>"
					+ ew + "</td></tr><tr><td class='s7 f8'>" + text_allianz + "</td><td class='s7 f8'>" + ally
					+ "</td></tr></table>";
			var tbt2 = "<table class='f8' width='100%' cellspacing='1' cellpadding='2' bgcolor='#F0F0F0'><tr bgcolor='#FFFFFF'><td class='c b' colspan='2' align='center'></a>"
					+ text_details
					+ "</td></tr><tr bgcolor='#FFFFFF'><td width='45%' class='c s7'>"
					+ text_spieler
					+ "</td><td class='c s7'>-</td></tr><tr bgcolor='#FFFFFF'><td class='c s7'>"
					+ text_einwohner
					+ "</td><td class='c s7'>-</td></tr><tr bgcolor='#FFFFFF'><td class='c s7'>"
					+ text_allianz
					+ "</td><td class='c s7'>-</td></tr></table>";
			if (spieler != null) {
				myElement.innerHTML = tbt;
			} else {
				myElement.innerHTML = tbt2;
			}
		}
	}

	var x_y = function(x, y) {
		mapDoc.getElementById('x').firstChild.nodeValue = x;
		mapDoc.getElementById('y').firstChild.nodeValue = y;
	}

	this.doParse = function(pasedDoc, parsedWindow) {
		var kxImgs = pasedDoc.evaluateXPath(TrvnCfg.queryXPath("mapImageDiv"))[0].getElementsByTagName("img");
		var kxAreas = pasedDoc.evaluateXPath(TrvnCfg.queryXPath("mapMap"))[0].getElementsByTagName("area");
		var dataArray = parsedWindow.m_c.ad;
		var l, t, ib = 0, jb = 0, ie = 7, je = 7;
		switch (pos) {
			case 1 :
				l = 216;
				t = 100;
				break;
			case 2 :
				l = 437;
				t = 220;
				ib = 1;
				break;
			case 3 :
				l = 1;
				t = 220;
				je = 6;
				break;
			case 4 :
				l = 223;
				t = 340;
				ib = 1;
				je = 6;
				break;
		}
		for (var i = ib;i < ie; i++)
			for (var j = jb;j < je; j++) {
				var lt = l + 36 * j + i * 37;
				var tt = t - 20 * j + i * 20;
				var newImg = mapDoc.createElement("img");
				newImg.style.position = "absolute";
				newImg.style.left = lt + "px";
				newImg.style.top = tt + "px";
				newImg.src = kxImgs[i * 7 + j].src;
				mapDiv.appendChild(newImg);
				var newArea = mapDoc.createElement("area");
				newArea.style.cursor = "pointer";
				newArea.shape = "poly";
				newArea.href = "#";
				(function(data) {
					newArea.addEventListener('mouseover', function() {
						try {
							map(data.name, data.dname, data.ew, data.ally, data.x, data.y);
						} catch (e) {
							TrvnUtils.Logger.error("big-map.js 401: " + e);
						}
					}, true);
				})(dataArray[i][j]);
				(function(data) {
					newArea.addEventListener('mouseout', function() {
						try {
							map(data.name, null, data.ew, data.ally, data.x, data.y);
						} catch (e) {
							TrvnUtils.Logger.error("big-map.js 402: " + e);
						}
					}, true);
				})(dataArray[i][j]);
				var clickJs = kxAreas[i * 7 + j].href;
				if (clickJs) {
					addEvent(newArea, "click", "Travian.travianDoc.location.href='" + clickJs + "'");
				}
				newArea.coords = (lt + 10) + "," + (tt + 53) + "," + (lt + 46) + "," + (tt + 33) + "," + (lt + 83)
						+ "," + (tt + 53) + "," + (lt + 46) + "," + (tt + 73);
				mapObj.appendChild(newArea);
			}
	}
}
function createMapDir(mapDoc, title, xp, yp) {
	var area = mapDoc.createElement("area");
	area.href = "#";
	area.style.cursor = "pointer";
	area.shape = "circle";
	area.title = title;
	var newXp = xp;
	var newYp = yp;
	switch (title) {
		case "东" :
			area.coords = "770,430,30";
			newXp += TrvnCfg.mapMoveSpeed;
			break;
		case "南" :
			area.coords = "210,430,30";
			newYp -= TrvnCfg.mapMoveSpeed;
			break;
		case "西" :
			area.coords = "200,115,30";
			newXp -= TrvnCfg.mapMoveSpeed;
			break;
		case "北" :
			area.coords = "762,115,30";
			newYp += TrvnCfg.mapMoveSpeed;
			break;
	}
	area.addEventListener('click', function() {
		try {
			setMapXY(mapDoc, newXp, newYp);
		} catch (e) {
			TrvnUtils.Logger.error("big-map.js 401:" + e);
		}
	}, false);
	return area;
}
function setMapXY(mapDoc, xp, yp) {
	createMap(mapDoc, xp, yp);
	var mapObj = mapDoc.getElementById("trvnmap");
	while (mapObj.firstChild) {
		mapObj.removeChild(mapObj.firstChild);
	}
	mapObj.appendChild(createMapDir(mapDoc, "东", xp, yp));
	mapObj.appendChild(createMapDir(mapDoc, "南", xp, yp));
	mapObj.appendChild(createMapDir(mapDoc, "西", xp, yp));
	mapObj.appendChild(createMapDir(mapDoc, "北", xp, yp));
	var mapDiv = mapDoc.getElementById("trvnMapDiv");
	while (mapDiv.firstChild) {
		mapDiv.removeChild(mapDiv.firstChild);
	}
	mapDoc.getElementsByName("xp")[0].value = xp;
	mapDoc.getElementsByName("yp")[0].value = yp;
	mapDoc.getElementById('x').firstChild.nodeValue = xp;
	mapDoc.getElementById('y').firstChild.nodeValue = yp;
	mapDoc.parseUrl(TrvnCfg.queryUrl("karte.php?xp=" + (xp - 3) + "&yp=" + (yp + 3)), new DrawMapParseCallback(1, mapDoc, xp, yp));
	mapDoc.parseUrl(TrvnCfg.queryUrl("karte.php?xp=" + (xp + 3) + "&yp=" + (yp + 3)), new DrawMapParseCallback(2, mapDoc, xp, yp));
	mapDoc.parseUrl(TrvnCfg.queryUrl("karte.php?xp=" + (xp - 3) + "&yp=" + (yp - 3)), new DrawMapParseCallback(3, mapDoc, xp, yp));
	mapDoc.parseUrl(TrvnCfg.queryUrl("karte.php?xp=" + (xp + 3) + "&yp=" + (yp - 3)), new DrawMapParseCallback(4, mapDoc, xp, yp));
}
function createMap(mapDoc, xp, yp) {
	mapDoc.title = "地图";
	mapDoc.body.innerHTML = "";
	var xyDiv = mapDoc.createElement("div");
	xyDiv.innerHTML = "<h1>地图(<span id='x'>" + xp + "</span>|<span id='y'>" + yp + "</span>)</h1>";
	xyDiv.style.position = "absolute";
	xyDiv.style.zIndex = 500;
	xyDiv.style.width = "200px";
	xyDiv.style.height = "80px";
	xyDiv.style.left = "10px";
	xyDiv.style.top = "10px";
	mapDoc.body.appendChild(xyDiv);
	var submitDiv = mapDoc.createElement("div");
	submitDiv.style.position = "absolute";
	submitDiv.style.zIndex = 500;
	submitDiv.style.left = "10px";
	submitDiv.style.top = "465px";
	var e = mapDoc.createElement("b");
	e.innerHTML = "X:";
	submitDiv.appendChild(e);
	e = mapDoc.createElement("input");
	e.name = "xp";
	e.size = 2;
	e.maxLength = 4;
	e.value = xp;
	submitDiv.appendChild(e);
	e = mapDoc.createElement("b");
	e.innerHTML = "Y:";
	submitDiv.appendChild(e);
	e = mapDoc.createElement("input");
	e.name = "yp";
	e.size = 2;
	e.maxLength = 4;
	e.value = yp;
	submitDiv.appendChild(e);
	e = mapDoc.createElement("img");
	e.src = TrvnCfg.queryUrl("img/cn/b/ok1.gif");
	e.border = 0;
	e.align = "absmiddle";
	e.style.cursor = "pointer";
	e.addEventListener('click', function() {
		setMapXY(mapDoc, TrvnUtils.parseNumber(mapDoc.getElementsByName("xp")[0].value), TrvnUtils.parseNumber(mapDoc.getElementsByName("yp")[0].value));
	}, false);
	submitDiv.appendChild(e);
	mapDoc.body.appendChild(submitDiv);
	var infoDiv = mapDoc.createElement("div");
	infoDiv.id = "tb";
	infoDiv.align = "center";
	infoDiv.style.position = "absolute";
	infoDiv.style.zIndex = 500;
	infoDiv.style.width = "170px";
	infoDiv.style.height = "80px";
	infoDiv.style.right = "20px";
	infoDiv.style.top = "20px";
	infoDiv.innerHTML = "<table class='f8' width='100%' cellspacing='1' cellpadding='2' bgcolor='#F0F0F0'><tr bgcolor='#FFFFFF'><td class='c b' colspan='2' align='center'></a>详情:</td></tr><tr bgcolor='#FFFFFF'><td width='45%' class='c s7'>玩家:</td><td class='c s7'>-</td></tr><tr bgcolor='#FFFFFF'><td class='c s7'>居民:</td><td class='c s7'>-</td></tr><tr bgcolor='#FFFFFF'><td class='c s7'>联盟:</td><td class='c s7'>-</td></tr></table>";
	mapDoc.body.appendChild(infoDiv);
	var mapDiv = mapDoc.createElement("div");
	mapDiv.id = "trvnMapDiv";
	mapDiv.align = "center";
	mapDiv.style.position = "absolute";
	mapDiv.style.zIndex = 50;
	mapDiv.style.left = "10px";
	mapDiv.style.top = "0px";
	mapDoc.body.appendChild(mapDiv);
	var mapObj = mapDoc.createElement("map");
	mapObj.id = "trvnmap";
	mapObj.name = "trvnmap";
	mapDoc.body.appendChild(mapObj);
	var mapImg = mapDoc.createElement("img");
	mapImg.style.position = "absolute";
	mapImg.style.width = "975px";
	mapImg.style.height = "550px";
	mapImg.style.right = "20px";
	mapImg.style.top = "20px";
	mapImg.style.left = "0px";
	mapImg.style.top = "0px";
	mapImg.style.zIndex = 400;
	mapImg.src = TrvnCfg.queryUrl("img/un/m/bg_xxl.gif");
	mapImg.useMap = "#trvnmap";
	mapImg.border = 0;
	mapDoc.body.appendChild(mapImg);
}